<?php

namespace app\user\controller;

use app\admin\service\UserService;
use app\BaseController;
use app\model\ConsultOfflineModel;
use app\model\ConsultOnlineModel;
use app\model\LogModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 咨询消息
     * @return bool|string
     */
    public function ConsultMessage(): bool|string
    {
        try {
            $ConsultOnlineModel = new ConsultOnlineModel();
            $ConsultOfflineModel = new ConsultOfflineModel();
            $data = $ConsultOnlineModel
                ->field('id,question,time,1 as type,uid')
                ->where('uid','=',$this->userData['id'])
                ->with(['user'])
                ->order('id desc')->limit(0,4)->select()->toArray();
            $data2 = $ConsultOfflineModel
                ->field('id,question,time,2 as type,uid,time_id')
                ->where('uid','=',$this->userData['id'])
                ->with(['user', 'ConsultTime'])
                ->order('id desc')->limit(0,4)->select()->toArray();
            foreach ($data2 as &$value){
                $value['date'] = date('Y-m-d',$value['reservation_start_time']);
                $value['period'] = date('H:i',$value['reservation_start_time']).'-'.date('H:i',$value['reservation_end_time']);
            }
            $data3 = array_merge($data,$data2);
            $field = array_column($data3,'time');
            array_multisort($field, SORT_DESC, $data3);
            $data4 = array_slice($data3, 0, 4);
            $UserService = new UserService($this->app);
            $NotDelDepartmentAll = $UserService->getNotDelDepartmentAffiliationAll($this->userData['unit_id']);
            foreach ($data4 as &$item){
                $item['department_name'] = $NotDelDepartmentAll[$item['department_id']]['all_name'];
            }
            return success($data4);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 系统日志
     * @return bool|string
     */
    public function LogList(): bool|string
    {
        try {
            $param = $this->request->param();
            $where = [];
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['type'])){
                $where[] = ['type','in',$param['type']];
            }
            $where[] = ['uid','=',$this->userData['id']];
            $LogModel = new LogModel();
            $data = $LogModel->where($where)->with(['userName'])->order('id desc')->paginate($param['pageSize'])->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


}